package com.yuxi.Util;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

public class IPUtil {
	public static String getIpAddr(HttpServletRequest request) {  
        String ip = request.getHeader("X-Forwarded-For");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }  
	
	public String getLocationAddress(ServletRequest request,ServletContext context){
		//apache  
		String ipport = System.getProperty("httpd.runtime.server"); 
		if(null == ipport){   
			//weblogic    
			ipport = System.getProperty("weblogic.runtime.server");
			//if(null == ipport){   
				//单机环境   
		//	request.getServletContext().getRequest().getLocalAddr();
			context.getClassLoader();
			request.getLocalAddr();
				//ipport =  "http://"+ServletActionContext.getRequest().getLocalAddr()+":"+ServletActionContext.getRequest().getLocalPort();  
			//} 
		} 
		return ipport;
	}
}
